package org.javaboy.webflux_r2dbc.handler;

import org.javaboy.webflux_r2dbc.dao.UserDao;
import org.javaboy.webflux_r2dbc.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import static org.springframework.web.reactive.function.server.ServerResponse.ok;

/**
 * User 处理器，作用有点类似于 SpringMVC 中的 Controller，相当于 Controller 的方法（不包含 Controller 中方法的注解）
 */
@Component
public class UserHandler {
    @Autowired
    UserDao userDao;

    public Mono<ServerResponse> addUser(ServerRequest request) {
        return ok().contentType(MediaType.APPLICATION_JSON)
                .body(userDao.saveAll(request.bodyToMono(User.class)), User.class);
    }

    public Mono<ServerResponse> getUser(ServerRequest request) {
        return ok().contentType(MediaType.APPLICATION_JSON).body(userDao.findAll(), User.class);
    }
}
